<template>
  <van-popup
    class="vpopup"
    v-model="popupShow"
    :position="position"
    :overlay="overlay"
    :get-container="getContainer"
    :style="popupStyle"
  >
    <div class="wrap">
      <div class="head">
        <div><slot name="title"></slot></div>
        <span class="btn" @click="togglePopup"><i class="fa fa-close"></i></span>
      </div>
      <div class="body">
        <slot></slot>
      </div>
    </div>
  </van-popup>
</template>

<script>
import { Popup as VanPopup } from 'vant'
export default {
  name: 'Popup',
  components: {
    VanPopup
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    // 是否遮罩
    overlay: {
      type: Boolean,
      default: false
    },
    // 弹出方向
    position: {
      type: String,
      default: 'bottom'
    },
    // 挂载位置
    getContainer: {
      type: String,
      default: '#app'
    },
    // 弹层样式控制
    popupStyle: {
      type: Object
    }
  },
  data () {
    return {
      popupShow: this.show
    }
  },
  watch: {
    show (newVal) {
      this.popupShow = newVal
    },
    popupShow (newVal) {
      this.$emit('update:show', newVal)
    }
  },
  methods: {
    togglePopup () {
      this.popupShow = !this.popupShow
    }
  }
}
</script>

<style lang="scss" scoped>
  .vpopup{
    top: 0;
  }
  .head{
    position: sticky;
    top: 0;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 rem(10px);
    font-size: rem(15px);
    height: rem(44px);
    background-color: #fff;
    .btn {
      display: flex;
      align-items: center;
      height: 100%;
      .fa{
        font-size: rem(22px);
        color: $color-text-secondary;
      }
    }
  }
  .body{
    padding: {
      left: rem(10px);
      right: rem(10px);
    }
  }
</style>
